<template>
  <view class="container">
    <u-navbar @click="backIndex" back-icon-color="#fff" title-color="#fff" title="联想背题" :background="background">
    </u-navbar>
    <view class="container_tag">
      <u-tag :text="item.name" @click="lenovoHandle(item,index)" :type="index === tagIndex ? 'success' : 'info'"
        v-for="(item,index) in tagList" />
    </view>
  </view>
</template>

<script>
  import {
    queryAIAssociateAPI,
    getExamAPI
  } from '@/api/addPaper/addpaper.js'
  import store from '@/uni_modules/sv-exam-plus/store/index.js'
  export default {
    data() {
      return {
        background: {
          backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
        },
        personId: null,
        tagList: [],
        questionBankType: 3,
        categoryId: '',
        xxx: null,
        questionIds: null,
        tagIndex:'',
        associated:''
      };
    },
    onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
      this.personId = option.id
      this.categoryId = option.categoryId
    },
    created() {
      this.queryAIAssociateList()
    },
    methods: {
      async queryAIAssociateList() {
        const param = {
          id: this.personId,
          associateType: ''
        }
        await queryAIAssociateAPI(param).then((res) => {
          this.tagList = res.data.map(item => {
            const key = Object.keys(item)[0];
            const value = item[key];
            return {
              ...value,
              name: key
            };
          });

        })
      },
      getExamList() {
        return new Promise((resolve, reject) => {
          const param = {
            categoryId: this.categoryId,
            questionBankType: this.questionBankType,
            questionIds: this.questionIds
          }
          getExamAPI(param).then((res) => {
            resolve(res.data)
            this.xxx = res.data
            this.associated = res.data.associated

          })
        })
      },
      async lenovoHandle(item,index) {
        this.tagIndex = index;
        this.questionIds = item.questionIds
        this.aIAssociate = item.name
        store.commit('updateaIAssociate', this.aIAssociate)
        this.examList = await this.getExamList();
        uni.navigateTo({
          url: '../../uni_modules/sv-exam-plus/pages/exam/exam',
          success: (res) => {
            res.eventChannel.emit('e-transmit-exams', {
              param: {
                from_lib: this.curLib,
                unit: 'unit-1'
              },
              opt: {
                lib_name: this.curLib,
                type: 'practice',
                questionCompletedCount: this.questionCompletedCount,
                questionBankType: this.questionBankType,
                categoryId: this.categoryId,
                associated:this.associated
              },
              apifunc: {
                examList: this.xxx
              }
            })
          }
        })


      }
    }
  }
</script>

<style lang="scss">
  .container {
    padding: 20rpx;

    .container_tag {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-evenly;
      align-items: center;

      .u-tag {
        width: 200rpx;
        text-align: center;
        margin: 5px;
        /* 根据需要调整间距 */
      }


    }


  }
</style>